﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    [Serializable]
    public class ListNewByGroupSaveItemCls
    {
        public string Title;
        public string GroupNewsItemId;
        public string IndexOrder;
        public string MaxItems;
        public bool  Checked=false;
    }

    public class AddListNewsByGroupItem : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AddListNewsByGroupItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới tin theo nhóm");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddListNewsByGroupItem));
            string ListNewsByGroupId = (string)System.Web.HttpContext.Current.Request["ListNewsByGroupId"];
            ListNewsByGroupCls ListNewsByGroupCls = new ListNewsByGroupBLL().CreateModel(Environment.Environments.ActiveLanguage, ListNewsByGroupId);
            string SelectGroupNews = "<select id=divSelectGroupNews onchange=\"javascript:DetectGroupNewsItem();\">";
            System.Data.DataTable dtGroupNews = new GroupNewsBLL().Reading(ListNewsByGroupCls.belongSite);
            string FirstGroupNewsId = "";
            for (int iIndex = 0; iIndex < dtGroupNews.Rows.Count; iIndex++)
            {
                string GroupNewsId=(string)dtGroupNews.Rows[iIndex]["GroupNewsId"];
                string Subject=(string)dtGroupNews.Rows[iIndex][Environment.Environments.ActiveLanguage+ "Subject"];
                SelectGroupNews += "<option value=\""+GroupNewsId+"\">" + Subject + "</option>";
                if (iIndex == 0) FirstGroupNewsId = GroupNewsId;
            }
            SelectGroupNews += "</select>";
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "ListNewsByGroup.js") +
                             Environment.Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "GroupNewsCaption", "Tin theo nhóm") + "</td>" +
                             "              <td>" + SelectGroupNews + "</td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddListNewsByGroupItem('" + ListNewsByGroupId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "AddListNewsByGroupItemSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddListNewsByGroupItemCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                             "<div id=divContentGroupNews>" + GetGroupNewsText(FirstGroupNewsId) + "</div>" +
                             "</div>", ResourceBLL.GetText(WebPartId, "AddListNewsByGroupItemFormTitle", "Thêm mới tin theo nhóm"));
            return RetHtml;
        }

      
        [AjaxPro.AjaxMethod]
        public string GetGroupNewsText(string GroupNewsId)
        {
            System.Data.DataTable dtGroupNewsItem = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, null);
            string RetHtml = "<div class=cssPlsCheckGroupNews>"+ResourceBLL.GetText(WebPartId,"PlsCheck","Tích vào nhóm")+"</div><ul class=cssUlGroupNews>";
            
            int iCount = 0;
            for (int iIndex = 0; iIndex < dtGroupNewsItem.Rows.Count; iIndex++)
            {
                string GroupNewsItemName = (string)dtGroupNewsItem.Rows[iIndex][Environment.Environments.ActiveLanguage+ "GroupNewsItemName"];
                string GroupNewsItemId = (string)dtGroupNewsItem.Rows[iIndex]["GroupNewsItemId"];
                RetHtml += "<li class=cssLiLevel1><input type=hidden id=txtGroupNewsItemId_" + iCount + " value=\"" + GroupNewsItemId + "\"><input type=checkbox id=checkBox_" + iCount + "><input id=txtTitle_" + iCount + " class=cssTitleItemLevel1 value=\"" + GroupNewsItemName + "\"><input class=cssIndexOrderItem id=txtIndexOrder_" + iCount + " value=\"" + (iCount + 1).ToString() + "\"><input class=cssMaxItem id=txtMaxItem_" + iCount + " value=\"0\"></li>";
                System.Data.DataTable dtGroupNewsSubItems = new GroupNewsBLL().ReadingGroupNewsItems(GroupNewsId, GroupNewsItemId);
                for (int zIndex = 0; zIndex < dtGroupNewsSubItems.Rows.Count; zIndex++)
                {
                    iCount++;
                    string SubGroupNewsItemName = (string)dtGroupNewsSubItems.Rows[zIndex][Environment.Environments.ActiveLanguage + "GroupNewsItemName"];
                    string SubGroupNewsItemId = (string)dtGroupNewsSubItems.Rows[zIndex]["GroupNewsItemId"];
                    RetHtml += "<li class=cssLiLevel2><input type=hidden id=txtGroupNewsItemId_" + iCount + " value=\"" + SubGroupNewsItemId + "\"><input type=checkbox id=checkBox_" + iCount + "><input id=txtTitle_" + iCount + " class=cssTitleItemLevel2 value=\"" + SubGroupNewsItemName + "\"><input class=cssIndexOrderItem id=txtIndexOrder_" + iCount + " value=\"" + (iCount + 1).ToString() + "\"><input class=cssMaxItem id=txtMaxItem_" + iCount + " value=\"0\"></li>";
                }
                iCount++;
            }
            RetHtml += "</ul><input type=hidden id=txtMaxItems value=\"" + iCount + "\">";
            return RetHtml;
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideAddListNewsByGroupItems(string ListNewsByGroupId, ListNewByGroupSaveItemCls[] SaveItems)
        {
            try
            {
                ListNewsByGroupCls ListNewsByGroupCls = new ListNewsByGroupBLL().CreateModel(Environment.Environments.ActiveLanguage, ListNewsByGroupId);
                for (int iIndex = 0; iIndex < SaveItems.Length; iIndex++)
                {
                    if (SaveItems[iIndex].Checked)
                    {
                        new ListNewsByGroupBLL().AddListNewsByGroupItem(
                            null,
                            ListNewsByGroupCls.belongSite,
                            ListNewsByGroupId,
                            SaveItems[iIndex].Title,
                            SaveItems[iIndex].GroupNewsItemId,
                            int.Parse(SaveItems[iIndex].IndexOrder),
                            int.Parse(SaveItems[iIndex].MaxItems));
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public ListNewByGroupSaveItemCls[] CreateSaveItemsObject(int Max)
        {
            ListNewByGroupSaveItemCls[] SaveItems = new ListNewByGroupSaveItemCls[Max];
            for (int iIndex = 0; iIndex < Max; iIndex++)
            {
                SaveItems[iIndex] = new ListNewByGroupSaveItemCls();
            }
            return SaveItems;
        }
    }
}